#include "StdAfx.h"

class UnCopyable
{
protected :

     UnCopyable() {}
    ~UnCopyable() {}

private :

    UnCopyable( const UnCopyable & );
    UnCopyable & operator=( const UnCopyable & );
};

class CSmartLock
    : private UnCopyable
{
public :

    CSmartLock(
        LPCRITICAL_SECTION pCriticalSection
    )   : m_pCriticalSection( pCriticalSection )
    {
        if( this->m_pCriticalSection )
            ::EnterCriticalSection( m_pCriticalSection );
    }

    ~CSmartLock( void ){
        if( this->m_pCriticalSection )
            ::LeaveCriticalSection( m_pCriticalSection );
    }

    operator bool() {
        return false;
    }

private :

    LPCRITICAL_SECTION m_pCriticalSection;
};

#define SMART_LOCK( x )     CSmartLock SmartLock( &x );
#define SYNCHRONIZED( x )   if( CSmartLock SmartLock = &x ) \
                                assert( FALSE );            \
                            else

/*
    usage SMARTR_LOCK( x )
        
        CRITICAL_SECTION CS;
        InitializeCriticalSection( &CS );

        void foo( void ) {
            SMART_LOCK( &CS );
            ...
        }

        DeleteCriticalSection( &CS );

    usage SYNCHRONIZED( x )

        CRITICAL_SECTION CS;
        InitializeCriticalSection( &CS );

        void foo( void ){
            SYNCHRONIZED( CS )
            {
                    ...
            }
        }
                
        DeleteCriticalSection( &CS );
*/